/**
 * 模型
 * 敌方坦克
 */
import AbstractModel from "./abstract/AbstractModel";
import {image} from "../service/image";

import {upperFirst} from 'lodash'
import config from "../config";
import player from "../canvas/Player";
import {EnumDirection} from "../enum/enumPosition";
import utils from "../utils";
import water from "../canvas/Water";
import wallBrick from "../canvas/WallBrick";
import wallSteel from "../canvas/WallSteel";
import bullet from "../canvas/Bullet";
import boss from "../canvas/Boss";
import tank from "../canvas/Tank";
import app from "../app";

export default class ModelTank extends AbstractModel implements IModel {
    name: string = 'player';

    // 画布实例
    canvas: ICanvas = player;
    // 事件是否绑定
    isBindEvent: boolean = false

    // 继承父类抽象方法:渲染贴图
    // 一些初始化自定义的动作、行为，都在这里进行
    render(): void {
        //初始化时永远朝上
        // this.direction = EnumDirection.top
        // 初始化模型
        super.draw()

        if (!this.isBindEvent && app.gameStatus == 'gameStart') {
            this.isBindEvent = true
            // 添加键盘监听事件，坦克方向改变
            document.addEventListener('keydown', this.changeDirection.bind(this))
            // 添加键盘监听事件，坦克移动
            document.addEventListener('keydown', this.move.bind(this))
            // 添加键盘监听事件，坦克发射子弹
            document.addEventListener('keydown', this.bulletLaunch.bind(this))
        }
    }

    //游戏结束判断
    isGameOver(): boolean {

        if (this.isBindEvent && app.gameStatus != 'gameStart') {
            this.isBindEvent = false
            document.removeEventListener('keydown', this.changeDirection.bind(this), false)
            document.removeEventListener('keydown', this.bulletLaunch.bind(this), false)
            document.removeEventListener('keydown', this.move.bind(this), false)
            return false
        }
        return app.gameStatus == 'gameStart';
    }

    bulletLaunch(event: KeyboardEvent) {
        if (this.isGameOver()) {
            // console.log(event);
            //发射子弹,空格,回车，小键盘回车
            (event.code === 'Space'
                || event.code === 'Enter'
                || event.code === 'NumpadEnter') && bullet.addPlayerBullet();
        }
    }

    /**
     * 方向改变
     * @param event 键盘对象
     */
    changeDirection(event: KeyboardEvent) {
        if (this.isGameOver()) {
            // console.log(event)
            switch (event.code) {
                case 'KeyW':
                case 'ArrowUp':
                    this.direction = EnumDirection.top
                    break;
                case 'KeyD':
                case 'ArrowRight':
                    this.direction = EnumDirection.right
                    break;
                case 'KeyS':
                case 'ArrowDown':
                    this.direction = EnumDirection.bottom
                    break;
                case 'KeyA':
                case 'ArrowLeft':
                    this.direction = EnumDirection.left
                    break;
            }
            // 绘制模型
            this.canvas.renderModels()
        }
    }

    //移动
    move(event: KeyboardEvent) {
        if (this.isGameOver()) {
            // ********************* 坐标更新 *********************
            let x = this.x;
            let y = this.y;
            switch (event.code) {
                case 'KeyW':
                case 'ArrowUp':
                    y -= 15
                    break;
                case 'KeyD':
                case 'ArrowRight':
                    x += 15
                    break;
                case 'KeyS':
                case 'ArrowDown':
                    y += 15
                    break;
                case 'KeyA':
                case 'ArrowLeft':
                    x -= 15
                    break;
            }
            if (utils.modelTouch(x, y, [
                ...water.models,// 水域
                ...wallBrick.models,// 砖墙
                ...wallSteel.models,// 钢墙
                ...boss.models, //boss
                ...tank.models //敌方坦克
            ]) || utils.isCanvasTouch(x, y)) {
                // 随机获取方向
            } else {
                this.x = x;
                this.y = y;
                // 绘制模型
                this.canvas.renderModels()
            }
        }
    }

    // 随机取用其中一个图片
    getImage(): HTMLImageElement {
        return image.get(`${this.name}${upperFirst(this.direction)}` as keyof typeof config.images)!
    }
}
